//
//  RAPEmoticonKeyboardMenuBar.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 1/7/16.
//  Copyright © 2016 RendezvousAuParadis. All rights reserved.
//

import UIKit
import FDStackView

enum RAPEmoticonType: Int {
    case Recent = 1_000
    case Default = 1_001
    case Emoji = 1_002
    case HuaHua = 1_003
}

class RAPEmoticonKeyboardMenuBar: FDStackView {
    
    // MARK: - 自定义属性
    var menuButtonClickedClosure: ((buttonType: RAPEmoticonType) -> ())?
    
    private var selectedButton: UIButton? {
        didSet {
            selectedButton?.selected = true
        }
    }

    func selectButtonBySection(section: Int) -> () {
        
        let button = viewWithTag(section + 1000) as? UIButton
            
            
        
        if button === selectedButton {
            return
        }
        
        selectedButton?.selected = false
//        button?.selected = true
        selectedButton = button
    }
    
    // MARK: - 系统方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        getReady()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func getReady() {
        
        distribution = .FillEqually
        
        addArrangedSubview(childButton("最近", backgroundImageName: "compose_emotion_table_left_normal", emoticonType: RAPEmoticonType.Recent))
        addArrangedSubview(childButton("默认", backgroundImageName: "compose_emotion_table_mid_normal", emoticonType: RAPEmoticonType.Default))
        addArrangedSubview(childButton("Emoji", backgroundImageName: "compose_emotion_table_mid_normal", emoticonType: RAPEmoticonType.Emoji))
        addArrangedSubview(childButton("浪小花", backgroundImageName: "compose_emotion_table_right_normal", emoticonType: RAPEmoticonType.HuaHua))
        /*
        一开始默认选中第2个按钮
        */
        selectButtonBySection(1)

    }
    
    private func childButton(title: String, backgroundImageName: String, emoticonType: RAPEmoticonType) -> UIButton {
        let button = UIButton(title: title, fontSize: 14, imageName: nil, backgroundImageName: backgroundImageName, target: self, action: "menuBarButtonClicked:")
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Selected)
        
        // 找出_normal的范围
        // 恶心
        let range = (backgroundImageName as NSString).rangeOfString("_normal")
        let selectedBackgroundImageName = "\((backgroundImageName as NSString).substringToIndex(range.location))_selected"
        
        let selectedBGImg = UIImage(named: selectedBackgroundImageName)
        button.setBackgroundImage(selectedBGImg?.slicedImage(), forState: UIControlState.Selected)
        button.tag = emoticonType.rawValue
        button.adjustsImageWhenHighlighted = false
        return button
    }
    
    
    // MARK: - 动作方法
    @objc private func menuBarButtonClicked(sender: UIButton) {
        
        if sender === selectedButton {
            return 
        }
        
//        sender.selected = true
        selectedButton?.selected = false
        selectedButton = sender
        // 执行回调block
        menuButtonClickedClosure?(buttonType: RAPEmoticonType(rawValue: sender.tag)!)
    }
}
